<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>微信回调页面</title>
    <!--导入vue和axios的js库,必须把vue.js导入写在axios的前面-->
    <script type="text/javascript" src="plugins/vue/dist/vue.js"></script>
    <script type="text/javascript" src="plugins/axios/dist/axios.js"></script>
    <script type="text/javascript" src="script/common.js"></script>
</head>
<body>
<div id="callBackDiv"></div>

<script type="text/javascript">
    new Vue({
        el:"#callBackDiv",
        data:{
            code:""
        },
        methods:{
            wxLogin(){
                // 携带code调用后端微信登录接口
                this.$http.post("/wechat/login/"+this.code)
                    .then(result => {
                        result = result.data;
                        if(result.success){
                            console.log("result"+result.ajaxObj)
                            // 后端请求成功,一个是登录成功,一个是跳转绑定页面
                            if(result.ajaxObj.message && result.ajaxObj.message == "noBind"){
                                // 说明当前微信用户是第一次登录,需要跳转绑定页面
                                let {accessToken,openid} = result.ajaxObj;
                                location.href = "/binder.html?accessToken="+accessToken+"&openid="+openid;
                            }else{
                                //说明登录成功,拿到后端返回的token和loginUser,并存入localStrage中
                                alert("登录成功!");
                                let {token,loginUser} = result.ajaxObj;
                                localStorage.setItem("token",token)
                                localStorage.setItem("loginUser",JSON.stringify(loginUser))
                                location.href = "/index.html";
                            }
                        }else{
                            alert("微信登录失败!"+result.msg);
                        }
                    })
            }
        },
        mounted(){
            // 页面一加载完毕,就要获取url中的code,并携带code调用后端接口
            let obj = url2Obj();
            this.code = obj.code;
            this.wxLogin();
        }
    })
</script>
</body>
</html>